package com.ojoin.trade.common_core.web.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.shiro.Principal;

/**
 * 表单验证（包含验证码）过滤类
 */
@Service
public class SystemLogOutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter {

	protected String getRedirectUrl(ServletRequest request, ServletResponse response, Subject subject) {
		Principal principal = CommonUtils.getPrincipal();
		SavedRequest beforeLogin=WebUtils.getAndClearSavedRequest(request);
		String loginType=IConstDef.loginType_IpayNow;
		//没有登录，直接退出处理
		if(null!=principal){
			loginType = principal.getLoginType();
		}else{
			if(null!=beforeLogin&&StringUtils.indexOfAny(beforeLogin.getRequestURI(), new String[]{"/operate","/sys"})!=-1){
				loginType=IConstDef.loginType_Factor;
			}
		}
		
		String login_ipaynow_Url="/trade/index.html";
		String login_factor_Url="/operate/operatelogin.html";
		
		String logOut_Url=login_ipaynow_Url;
		if(IConstDef.loginType_IpayNow.equals(loginType)){
			logOut_Url=login_ipaynow_Url;
		}else if(IConstDef.loginType_Factor.equals(loginType)){
			logOut_Url=login_factor_Url;
		}
		
		return logOut_Url;
	}
	
}